package FenxiaoService

import (
	"context"
	"gdshop-back-end-go/client-api/app/request/FenxiaoReq"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
)

func GetMemberWithdraw(ctx context.Context, parames *FenxiaoReq.GetMemberWithdraw) *response.JsonResponse {
	config, err := GetConfig()
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	jieguo := GetMemberWithdrawInfo(ctx, int(parames.MemberId))
	jieguo["lowest_withdraw"] = config.LowestWithdraw // 最低提现金额
	return response.SuccessByRequestMessageData(nil, "成功", jieguo)
}

// GetMemberGrandTotal 获取会员全部累计佣金
func GetMemberGrandTotal(ctx context.Context, memberId int) float64 {
	sum, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_order_details").Where(
		"status = 99 AND member_id = ?", memberId).Sum("paid*rate/100000")
	if err != nil {
		return 0
	}

	return sum
}

// GetMemberTotalWithdraw 累计提现
func GetMemberTotalWithdraw(ctx context.Context, memberId int) float64 {
	sum, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_withdraw").Where(
		"status = 99 AND member_id = ?", memberId).Sum("money")
	if err != nil {
		return 0
	}

	return sum
}

func GetMemberFrozenWithdraw(ctx context.Context, memberId int) float64 {
	sum, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_withdraw").Where(
		"status = 1 AND member_id = ?", memberId).Sum("money")
	if err != nil {
		return 0
	}

	return sum
}

// GetMemberWithdrawInfo 获取会员可用佣金
func GetMemberWithdrawInfo(ctx context.Context, memberId int) map[string]interface{} {
	fanhui := g.Map{}
	// 累计佣金
	grand_total := GetMemberGrandTotal(ctx, memberId)
	fanhui["grand_total"] = grand_total
	// 累计提现
	total_withdraw := GetMemberTotalWithdraw(ctx, memberId)
	fanhui["total_withdraw"] = total_withdraw
	// 提取中
	frozen_withdraw := GetMemberFrozenWithdraw(ctx, memberId)
	fanhui["frozen_withdraw"] = frozen_withdraw
	// 可提现
	fanhui["available_withdraw"] = grand_total - total_withdraw - frozen_withdraw

	return fanhui
}

// GetMemberWithdrawOrderInfo 获取会员分销订单信息
func GetMemberWithdrawOrderInfo(ctx context.Context, memberId int) map[string]float64 {
	fanhui := map[string]float64{}

	// 今日收入金额
	sum, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_order_details").Where(
		"member_id = ? AND status IN (1,90,99) AND create_at BETWEEN ? AND ?",
		memberId, gtime.Now().Format("Y-m-d"),
		gtime.Now().Format("Y-m-d 23:59:59")).Sum("paid")
	if err != nil {
		fanhui["today_money"] = -1
	} else {
		fanhui["today_money"] = sum
	}
	// 进行中订单金额
	sum, err = toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_order_details").Where(
		"member_id = ? AND status IN (1,90)", memberId).Sum("paid*rate/100000")
	if err != nil {
		fanhui["progress_money"] = -1
	} else {
		fanhui["progress_money"] = sum
	}
	// 总销售金额
	sum, err = toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_order_details").Where(
		"member_id = ? AND status IN (1,90,99)", memberId).Sum("paid")
	if err != nil {
		fanhui["total_order_money"] = -1
	} else {
		fanhui["total_order_money"] = sum
	}
	return fanhui
}

func GetTeamNum(ctx context.Context, memberId int) int {
	value, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "gd_fenxiao_user").Where("member_id", memberId).
		Fields("id").Value()
	if err != nil {
		return 0
	}
	fuId := value.Int()

	/*mumCount, err := toolsDb.GetUnSafaTable(ctx, "fenxiao_member_user_map").Fields("1").
		Where("fenxiao_user_id", fuId).Count()
	if err != nil {
		return 0
	}*/
	// 团队人数应该不包含推荐的会员
	fuCount, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "gd_fenxiao_user").Fields("1").
		Where("status = 1 AND (parent_id = ? OR parent_parent_id = ?)", fuId, fuId).Count()
	if err != nil {
		return 0
	}
	//return mumCount + fuCount
	return fuCount
}
